//package com.zq.vaccinum.filter;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.zq.vaccinum.constant.Constants;
//import com.zq.vaccinum.domain.R;
//import com.zq.vaccinum.util.JwtTokenUtil;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
//import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * @Author: zq
// * @Date: 2023/08/04/2:12
// * @Description:
// */
//public class JwtLoginFilter extends UsernamePasswordAuthenticationFilter {
//    private AuthenticationManager authenticationManager;
//    public JwtLoginFilter(AuthenticationManager authenticationManager){
//        this.authenticationManager = authenticationManager;
//        this.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/login","POSTR]"));
//    }
//
//    /**
//     * 拦截登录，获取表单的用户名和密码
//     */
//    @Override
//    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
//        return authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username,password));
//    }
//
//    /*
//    * 登录成功后调用的方法
//    * 返回token
//    * */
//    @Override
//    protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
//        // security登录成功封装的实体验证
//        UserDetails userDetails = (UserDetails) authResult.getPrincipal();
//        // 根据用户名生成token
//        Map<String, Object> claims = new HashMap<>();
//        claims.put(Constants.JWT_USERNAME,userDetails.getUsername());
//        claims.put(Constants.JWT_CREATE_TIME,new Date());
//        String token = JwtTokenUtil.createToken(claims);
//        response.setHeader("token",token);
//        response.setContentType("application/json;charset=utf-8");
//        response.getWriter().print(new ObjectMapper().writeValueAsString(R.ok("登录成功")));
//    }
//
//    /*
//    * 登录失败后调用的方法
//    * */
//    @Override
//    protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException, ServletException {
//        response.setContentType("application/json;charset=utf-8");
//        response.getWriter().print(new ObjectMapper().writeValueAsString(R.fail("登录失败")));
//    }
//}
